﻿using KINLO.MES.MESModels;
using KINLO.Model.InfoModel;
using KINLO.Model.MES;
using KINLO.WebApi.MESModels;
using KINLO.WebApi.WebServiceWorkOrder;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.MES
{
    public static class GeneralWebApi
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(GeneralWebApi));
        /// <summary>
        /// 生产统计
        /// </summary>
        /// <param name="counter"></param>
        /// <returns></returns>
        //public static int MESProductCountData(string url,MESProductDataModel counter)
        //{
        //    try
        //    {
        //        var sendmsg = JsonConvert.SerializeObject(counter);
        //        log.Info($"【MES生产统计上传】MES上传信息:{sendmsg}");
        //        var Result = WebAPIClass.HttpPost(url, sendmsg);
        //        log.Info($"【MES生产统计上传】MES上传返回信息:{Result}");
        //        if (Result != null)
        //        {
        //            var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
        //            if (rspResult.code == "00" && rspResult.message == "ok")
        //            {
        //                return 1;
        //            }
        //            else
        //            {
        //                return -1;
        //            }
        //        }
        //        else
        //        {
        //            return 2;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error($"【MES生产统计上传】MES上传发生错误:{ex.ToString()}");
        //        return 2;
        //    }
        //}
        /// <summary>
        /// 生产统计 小时
        /// </summary>
        /// <param name="counter"></param>
        /// <returns></returns>
        public static int MESProductCountDataa_Hour(string url, RootMESProduct_HourDataModel counter)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(counter);
                log.Info($"MES生产统计(小时)上传】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"MES生产统计(小时)上传】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok")
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Info($"MES生产统计(小时)上传】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
        /// <summary>
        /// 生产统计 天
        /// </summary>
        /// <param name="url"></param>
        /// <param name="counter"></param>
        /// <param name="machineName">设备名称</param>
        /// <returns></returns>
        public static int MESProductCountData_Day(string url, RootMESProduct_DayDataModel counter)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(counter);
                log.Info($"MES生产统计(天)上传】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"MES生产统计(天)上传】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok")
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Info($"MES生产统计(小时)上传】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
        /// <summary>
        /// 设备报修
        /// </summary>
        /// <param name="eqinfo"></param>
        /// <returns></returns>
        public static int MESEquipmentMaintain(string url, MESEquipmentMaintainModel eqinfo)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(eqinfo);
                log.Info($"【MES设备报修接口】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"【MES设备报修接口】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok" && rspResult.success == "true")
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Error($"【MES设备报修接口】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
        /// <summary>
        /// 设备履历
        /// </summary>
        /// <param name="eqstatus"></param>
        /// <returns></returns>
        public static int MESEquipmentStatus(string url, List<MESEquipmentStatusModel> eqstatus)
        {
            try
            {
                MESEquipmentStatusRoot root = new MESEquipmentStatusRoot();
                root.data.AddRange(eqstatus);
                var sendmsg = JsonConvert.SerializeObject(root);
                log.Info($"【MES设备履历接口】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"【MES设备履历接口】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok" && rspResult.success == "true")
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Error($"【MES设备履历接口】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
        /// <summary>
        /// 标准参数
        /// </summary>
        /// <param name="generaldatamodel"></param>
        /// <returns></returns>
        public static object MESGeneralData(string url, MESGeneralDataModel generaldatamodel)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(generaldatamodel);
                log.Info($"【MES标准参数接口】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"【MES标准参数接口】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok" && rspResult.success == "true")
                    {
                        return rspResult.data;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Error($"【MES标准参数接口】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
        /// <summary>
        /// 工单获取
        /// </summary>
        /// <param name="workordermodel"></param>
        /// <returns></returns>
        public static object MESWorkOrder(string url, MESWorkOrderModel workordermodel)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(workordermodel);
                log.Info($"【MES工单获取接口】MES上传信息:{sendmsg}");
                JGWebService jGWeb = new JGWebService();
                jGWeb.Url = url;
                jGWeb.Timeout = 10000;
                var workorder = jGWeb.List_WorkOrder(workordermodel.process_id, workordermodel.equipment_id);
                //string workorder = "{\"success\":true,\"returnMsg\":\"\",\"statusCode\":\"200\",\"resultData\":[{\"Bill_No\":\"SF23010301001\",\"ProductionOrderNO\":\"SF230103010\",\"PC_No\":\"12345\",\"Material_Code\":\"DC20201 - 1816 - 002\",\"Material_ID\":\"1002001110014625\",\"Material_Spec\":\"18650H - 1600 - 3.2V / 高温型\",\"WorkOrderStatus_Name\":\"生产中\",\"EquipmentCode\":\"DZDX - 1 - 0349\",\"PlanedStart\":\"2023 - 01 - 07\",\"PlanedEndDate\":\"2023 - 01 - 07\"},{\"Bill_No\":\"SF23011001401\",\"ProductionOrderNO\":\"SF230110014\",\"PC_No\":\"6666666\",\"Material_Code\":\"DC20201 - 1811 - 002\",\"Material_ID\":\"1001911300110585\",\"Material_Spec\":\"18650 - 1100 - 3.2V\",\"WorkOrderStatus_Name\":\"生产中\",\"EquipmentCode\":\"DZDX - 1 - 0349\",\"PlanedStart\":\"2023 - 01 - 13\",\"PlanedEndDate\":\"2023 - 01 - 13\"},{\"Bill_No\":\"SF23022101201\",\"ProductionOrderNO\":\"SF230221012\",\"PC_No\":\"7777777\",\"Material_Code\":\"DC20201 - 1816 - 002\",\"Material_ID\":\"1002001110014625\",\"Material_Spec\":\"18650H - 1600 - 3.2V / 高温型\",\"WorkOrderStatus_Name\":\"生产中\",\"EquipmentCode\":\"DZDX - 1 - 0349\",\"PlanedStart\":\"2023 - 03 - 04\",\"PlanedEndDate\":\"2023 - 03 - 04\"}]}";
                log.Info($"【MES工单获取接口】MES上传返回信息:{workorder}");
                if (workorder != null)
                {
                    return workorder;
                }
                else
                {
                    return null;
                }
                //$"【MES工单获取接口】MES上传信息:{sendmsg}".Info();
                //var Result = WebAPIClass.HttpPost(url, sendmsg);
                //$"【MES工单获取接口】MES上传返回信息:{Result}".Info();
                //if (Result != null)
                //{
                //    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                //    if (rspResult.code == "00" && rspResult.message == "ok" && rspResult.success == "true")
                //    {
                //        return rspResult.data;
                //    }
                //    else
                //    {
                //        return null;
                //    }
                //}
                //else
                //{
                //    return null;
                //}
            }
            catch (Exception ex)
            {
                log.Error($"【MES工单获取接口】MES上传发生错误:{ex.ToString()}");
                return null;
            }
        }
        /// <summary>
        /// 工号校验
        /// </summary>
        /// <param name="workNameModel"></param>
        /// <returns></returns>
        public static int MESUserNameCheck(string url, MESWorkNameModel workNameModel)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(workNameModel);
                log.Info($"【MES工号校验获取接口】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"【MES工号校验获取接口】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok" && rspResult.success == "true")
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Error($"【MES工号校验获取接口】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
        /// <summary>
        /// 参数超限
        /// </summary>
        /// <param name="parmlimitmodel"></param>
        /// <returns></returns>
        public static object MESParmLimit(string url, MESParmLimitModel parmlimitmodel)
        {
            try
            {
                var sendmsg = JsonConvert.SerializeObject(parmlimitmodel);
                log.Info($"【MES参数超限接口】MES上传信息:{sendmsg}");
                var Result = WebAPIClass.HttpPost(url, sendmsg);
                log.Info($"【MES参数超限接口】MES上传返回信息:{Result}");
                if (Result != null)
                {
                    var rspResult = JsonConvert.DeserializeObject<MESResultModel>(Result);
                    if (rspResult.code == "00" && rspResult.message == "ok" && rspResult.success == "true")
                    {
                        return rspResult.data;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception ex)
            {
                log.Error($"【MES参数超限接口】MES上传发生错误:{ex.ToString()}");
                return 2;
            }
        }
    }
}
